package com.dmx.net;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.commons.net.SocketFactory;

public class SocketFactoryTimeOut implements SocketFactory {
	 private int __timeout;
	 
	 public SocketFactoryTimeOut(int timeout) {
	  this.__timeout = timeout;
	 }

	 public Socket createSocket(String host, int port)
	   throws UnknownHostException, IOException {
	  Socket socket = new Socket();
	  socket.setSoTimeout(__timeout);
	  socket.connect(new InetSocketAddress(host, port), __timeout );
	  return socket;
	 }

	 public Socket createSocket(InetAddress address, int port)
	   throws IOException {
	  Socket socket = new Socket();
	  socket.setSoTimeout(__timeout);
	  socket.connect(new InetSocketAddress(address, port), __timeout );
	  return socket;
	 }

	 public Socket createSocket(String host, int port, InetAddress localAddr,
	   int localPort) throws UnknownHostException, IOException {
	  return createSocket(host, port);
	 }

	 public Socket createSocket(InetAddress address, int port,
	   InetAddress localAddr, int localPort) throws IOException {
	  return createSocket(address, port);
	 }

	 /***
	     * Creates a ServerSocket bound to a specified port.  A port
	     * of 0 will create the ServerSocket on a system-determined free port.
	     * <p>
	     * @param port  The port on which to listen, or 0 to use any free port.
	     * @return A ServerSocket that will listen on a specified port.
	     * @exception IOException If an I/O error occurs while creating
	     *                        the ServerSocket.
	     ***/
	    public ServerSocket createServerSocket(int port) throws IOException
	    {
	        return new ServerSocket(port);
	    }

	    /***
	     * Creates a ServerSocket bound to a specified port with a given
	     * maximum queue length for incoming connections.  A port of 0 will
	     * create the ServerSocket on a system-determined free port.
	     * <p>
	     * @param port  The port on which to listen, or 0 to use any free port.
	     * @param backlog  The maximum length of the queue for incoming connections.
	     * @return A ServerSocket that will listen on a specified port.
	     * @exception IOException If an I/O error occurs while creating
	     *                        the ServerSocket.
	     ***/
	    public ServerSocket createServerSocket(int port, int backlog)
	    throws IOException
	    {
	        return new ServerSocket(port, backlog);
	    }

	    /***
	     * Creates a ServerSocket bound to a specified port on a given local
	     * address with a given maximum queue length for incoming connections.
	     * A port of 0 will
	     * create the ServerSocket on a system-determined free port.
	     * <p>
	     * @param port  The port on which to listen, or 0 to use any free port.
	     * @param backlog  The maximum length of the queue for incoming connections.
	     * @param bindAddr  The local address to which the ServerSocket should bind.
	     * @return A ServerSocket that will listen on a specified port.
	     * @exception IOException If an I/O error occurs while creating
	     *                        the ServerSocket.
	     ***/
	    public ServerSocket createServerSocket(int port, int backlog,
	                                           InetAddress bindAddr)
	    throws IOException
	    {
	        return new ServerSocket(port, backlog, bindAddr);
	    }
	}


